<?php
/**
 * 函数库
 * @copyright	http://www.easymvc.com
 * @author 		光哥 <439884988@qq.com>
 * @version		1.0 - 2012-06-29
 * @package		Lib
 */
if(!defined('EASYMVC')) {
	exit('Access Denied');
}

/**
 * 基本函数 
 */
class Lib_Func {
	
	static function convert($size)
	{
		$unit=array('b','kb','mb','gb','tb','pb');
		return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
	}
	
	
	/**
	 * 随机数
	 * @return string
	 */
	
	static function generateRand($length)
	{
		$string = '';
		$uppercase  = range('A', 'Z');
		$lowercase  = range('a', 'z');
		$numeric    = range(0, 9);
	
		$CharPool   = array_merge($uppercase, $numeric,$lowercase);
		$PoolLength = count($CharPool) - 1;
	
		for ($i = 0; $i < $length; $i++)
		{
	
		$string .= $CharPool[mt_rand(0, $PoolLength)];
	
		}
	
	
		return $string;
	}
	
	/**
	 * 程序执行时间
	 * 
	 * @return string
	 * 
	 */
	static function  micro_time() {
    	$temp = explode(" ", microtime());
    	return bcadd($temp[0], $temp[1], 6);
	}
	/**
	 * 获取字符串长度
	 *
	 * @param string $string
	 * @param type  $type=2 中文算2个字符  | $type=1 中文算1个字符
	 * @return integer
	 */
	static public function strlen($string,$type=2){
		return $type == 2 ? mb_strwidth($string,'utf-8') :mb_strlen($string,'utf-8');
	}
	
	
	/**
	 * 中英文混杂字符串截取
	 *
	 * @param string $string
	 * 原字符串
	 * @param interger $length
	 * 截取的字符数
	 * @param string $etc
	 * 省略字符
	 * @param string $charset
	 * 原字符串的编码
	 *
	 * @return string
	 */
	static public function substr_cn($string, $length = 80, $type=2, $etc = '...') {
		if(self::strlen($string,$type) < $length) return $string;
		$func = $type == 2 ? 'mb_strimwidth' : 'mb_substr';
		if($type == 2)$string = mb_strimwidth($string,0,$length,'','utf-8');
		else $string = mb_substr($string,0,$length,'utf-8');
		$string .=  $etc;
	
		return $string;
	}
	
	/*
	 * 格式化时间
	*
	* @param string $time
	* @return string  "%s秒前" | "%s分钟前" | "今天 %s" |"%s-%s-%s"
	*/
	public static function timeFormat($time) {
		$format = array(
				"%s秒前","%s分钟前","今天 %s","%s-%s-%s","%s月%s日"
		);
		$now = time();
		$time = strtotime($time);
		$dur = $now - $time;
		if($dur < 0) return false;
		if($dur < 3600) {
			if($dur<50){
				$second = ceil($dur / 10)*10;
				if ($second<=0){
					$second = 10;
				}
				$time = sprintf($format[0], $second);
			}else{
				echo 'ff';
				$minutes = ceil($dur / 60);
				if ($minutes<=0){
					$minutes = 1;
				}
				$time = sprintf($format[1], $minutes);
			}
		}else
			if(date("Ymd", $now) == date("Ymd", $time)) {
			$time = sprintf($format[2], date("H:i", $time));
		}else{
			$time = sprintf($format[3], date("Y",$time),date("n",$time),date("j",$time));
		}
		return $time;
	}
	
	/**
	 * 获取客户端ip
	 *
	 * @return string
	 * 
	 */
	static  public function getIp() {
		// Gets the default ip sent by the user
		if (!empty($_SERVER['REMOTE_ADDR'])) {
			$step = 1;
			$direct_ip = $_SERVER['REMOTE_ADDR'];
		}

		// Gets the proxy ip sent by the user
		$proxy_ip     = '';
		if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
			$step = 2;
			$proxy_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
		} else if (!empty($_SERVER['HTTP_X_FORWARDED'])) {
			$step = 3;
			$proxy_ip = $_SERVER['HTTP_X_FORWARDED'];
		} else if (!empty($_SERVER['HTTP_FORWARDED_FOR'])) {
			$step = 4;
			$proxy_ip = $_SERVER['HTTP_FORWARDED_FOR'];
		} else if (!empty($_SERVER['HTTP_FORWARDED'])) {
			$step = 5;
			$proxy_ip = $_SERVER['HTTP_FORWARDED'];
		} else if (!empty($_SERVER['HTTP_VIA'])) {
			$step = 6;
			$proxy_ip = $_SERVER['HTTP_VIA'];
		} else if (!empty($_SERVER['HTTP_X_COMING_FROM'])) {
			$step = 7;
			$proxy_ip = $_SERVER['HTTP_X_COMING_FROM'];
		} else if (!empty($_SERVER['HTTP_COMING_FROM'])) {
			$step = 8;
			$proxy_ip = $_SERVER['HTTP_COMING_FROM'];
		}

		$ip = $direct_ip;		
		return $ip;
	}
	/**
	 * IP访问限制检测
	 *
	 * @param array $allow_ip
	 * @return boolean
	 */
	public static function allow_visit($allow_ip='') {
		if (empty($allow_ip)) $allow_ip = Frame_Config::get('allow_visit_ip');
		$ip = self::getIp();
		if ($ip == "" || empty($allow_ip) ) return true;
		
		foreach($allow_ip as $sub) {
			if($ip == $sub) return true;
		}
		return false;
	}
    
	
	/**
	 * 检测整数范围
	 * 
	 * @param integer $integer
	 * @param integer $from
	 * @param integer $to
	 * @return boolean
	 */
	public static function checkIntRange($integer,$from = "",$to = "") {
		if (! is_numeric ( $integer )) {
			return false;
		}
		if ($from != "" && $integer < $from) {
			return false;
		}
		if ($to != "" && $integer > $to) {
			return false;
		}
		return true;
	}

    /*
     * 时间差
     * 
     * @param string $starget 
     * @return string
     */
	static function diffdate($target) {
		$time = round((strtotime($target) - strtotime(date("Y-m-d")))/86400);
		return $time;
	}

	/**
	 * 将<、>、&、转换进行html字符编码
	 *
	 * @param string $esctext
	 * 转换后的字符串
	 * @param string $text
	 * 转换前的字符串
	 */
	static public function html_escape($text) {
		return htmlspecialchars($text,ENT_QUOTES);
	}

	/**
	 * 去掉字符串前后的空格(半全角空格)
	 *
	 * @param string $str
	 * return 处理后的文本
	 */
	static public function trim_cn($str) {
		$str = ' '.$str;
		return preg_replace('/(^[\s\x{3000}]*)|([\s\x{3000}]*$)/u','',$str);
	}

	
	/**
	 * 中英文混杂字符串截取
	 * @param string $string 原字符串
	 * @param interger $length 截取字数(中英各代表一个字)
	 * @param string $charset 编码
	 */
	static public function split_str2array_cn($string, $length = 80, $charset = 'UTF-8') {
		$i = $k = $nextstart = 0;

		$split_count = ceil(strlen($string)/($length*2));
		for($ii=0;$ii<$split_count;$ii++) {
			$en = $cn = 0;
			while($k < $length) {
				if (preg_match ("/[0-9a-zA-Z]/", $string[$i])){
					$en++;   	//纯英文
				}
				else {
					$cn++;     //非英文字节
				}
				$k = $cn/3+$en/2;
				$i++;
			}
				
			$split_len += $cn/3+$en;	//最终截取长度
			$start = $nextstart;
			$nextstart += floor($split_len);
			$tmpstr = mb_substr($string,$start,$split_len,$charset);
			if(!empty($tmpstr)) {
				$split_array[] = mb_substr($string,$start,$split_len,$charset);
			}
		}
		return $split_array;
	}
}
?>
	